<?php

namespace My\Controller;

use Utuo\BackgroundController;

class WechatReplyController extends BackgroundController {
	// 初始化
	public function _initialize() {
		$this->setControllerTitle('自动回复设置');
		$this->setActionTitle(array(
				'subscribe' => '关注自动回复',
				'anything' => '任意消息自动回复',
				'menu' => '菜单点击自动回复',
				'index' => '关键词规则列表',
				'add' => '添加关键词规则',
				'modify' => '修改关键词规则',
				'set' => '设置回复内容' 
		));
		$this->setActionNav(array(
				'subscribe',
				'anything',
				'index',
				'add' 
		));
	}
	
	// 规则列表
	public function indexAction() {
		$d = D("WechatReply");
		$types = $d->replyTypes;
		$condition = array(
				'cid' => $this->aid,
// 				'rule' => array(
// 						'not in',
// 						array(
// 								$d->ruleSubscribe,
// 								$d->ruleAnything,
// 								$d->ruleMenu
// 						) 
// 				) 
		);
		$keywordReplyLists = $d->where($condition)->limit($this->pageSize)->order('id desc')->page($this->getPage())->select();
		foreach ($keywordReplyLists as $k => $v) {
			$keywordReplyLists[$k]['type'] = $types[$v['type']];
			$keywordReplyLists[$k]['keyword'] = trim($v['keyword'], ' ,');
		}
		$count = $d->where($condition)->count();
		$this->assign('keywordReplyLists', $keywordReplyLists);
		$this->showPage($count, $this->pageSize);
		$this->display();
	}
	
	// 添加关键词规则
	public function addAction() {
		C('TOKEN_ON', true);
		$this->assign('typeOptions', $this->getOption(D('WechatReply')->replyTypes, 'text'));
		$this->display();
	}
	
	// 修改关键词规则提交
	public function addPostAction() {
		C('TOKEN_ON', true);
		$d = D('WechatReply');
		if ($d->field("keyword,rule")->create()) {
			
			$d->cid = $this->aid;
			$d->keyword = $d->parseKeyword($d->keyword);
			unset($d->link_url);
			if (false !== $d->add()) {
				$this->ajaxSuccess('保存成功');
			} else {
				$this->ajaxError($d->getError().$d->getLastSql());
			}
		} else {
			$this->ajaxError($d->getError().'aaaa'.$d->getLastSql());
		}
	}
	
	// 修改关键词规则
	public function modifyAction() {
		$d = D('WechatReply');
		$reply = $this->getOneById('WechatReply');
		$this->checkAdmin($reply['cid']);
		$reply['keyword'] = trim($reply['keyword'], ' ,');
		$this->reply = $reply;
		$this->display();
	}
	
	// 设置回复内容
	public function setAction() {
		$d = D('WechatReply');
		$reply = $this->getOneById('WechatReply');
		$this->checkAdmin($reply['cid']);
		$reply['keyword'] = preg_replace("/[\s+\']/", '', $reply['keyword']);
		$reply['typeOptions'] = $this->getOption($d->replyTypes, $reply['type']);
		$reply['news'] = M('WechatMaterial')->where(array(
				'id' => array(
						'in',
						$reply['news'] 
				),
				'cid' => $this->aid 
		))->select();
		$this->assign("newsJSON", empty($reply['news']) ? "{}" : json_encode(addID($reply['news'], 'id', 'id-')));
		$this->reply = $reply;
		$this->display();
	}
	
	// 修改关键词规则提交
	public function modifyPostAction() {
		$d = D('WechatReply');
		if ($d->field("id,keyword,rule")->create()) {
			$d->cid = $this->aid;
			$d->keyword = $d->parseKeyword($d->keyword);
			if (false !== $d->save()) {
				$this->ajaxSuccess('保存成功');
			} else {
				$this->ajaxError($d->getError());
			}
		} else {
			$this->ajaxError($d->getError());
		}
	}
	
	// 提交设置回复内容
	// 统一处理subscribe/anything/set的提交
	public function replyPostAction() {
		$d = D('WechatReply');
		if ($d->create()) {
			if (false !== $d->save()) {
				$this->ajaxSuccess('保存成功');
			} else {
				$this->ajaxError($d->getError());
			}
		} else {
			$this->ajaxError($d->getError());
		}
	}
	
	// 关注自动回复
	public function subscribeAction() {
		$d = D('WechatReply');
		$condition = array(
				'rule' => $d->ruleSubscribe,
				'cid' => $this->aid 
		);
		$reply = $d->where($condition)->find();
		if (!$reply) {
			$data = array(
					'rule' => $d->ruleSubscribe,
					'keyword' => $d->keywordSubscribe,
					'cid' => $this->aid 
			);
			$id = $d->add($data);
			if ($id) {
				$reply = $d->find($id);
			} else {
				$this->error('添加记录失败');
			}
		}
		
		$reply['typeOptions'] = $this->getOption($d->replyTypes, $reply['type']);
		$reply['newsLists'] = M('WechatMaterial')->where(array(
				'id' => array(
						'in',
						$reply['news'] 
				),
				'cid' => $this->aid 
		))->order("field(id," . $reply['news'] . ")")->select();
		$this->assign("newsJSON", empty($reply['newsLists']) ? "{}" : json_encode(addID($reply['newsLists'], 'id', 'id-')));
		$this->assign('reply', $reply);
		$this->display();
	}
	
	// 任意消息自动回复
	public function anythingAction() {
		$d = D("WechatReply");
		$condition = array(
				'rule' => $d->ruleAnything,
				'cid' => $this->aid 
		);
		$reply = $d->where($condition)->find();
		if (!$reply) {
			$data = array(
					'rule' => $d->ruleAnything,
					'keyword' => $d->keywordAnything,
					'cid' => $this->aid 
			);
			
			if ($d->add($data)) {
				$reply = $d->where($condition)->find();
			} else {
				$this->error('添加记录错误');
			}
		}
		
		$reply['typeOptions'] = $this->getOption($d->replyTypes, $reply['type']);
		$reply['newsLists'] = M('WechatMaterial')->where(array(
				'id' => array(
						'in',
						$reply['news'] 
				),
				'cid' => $this->aid 
		))->order("field(id," . $reply['news'] . ")")->select();
		$this->assign("newsJSON", empty($reply['newsLists']) ? "{}" : json_encode(addID($reply['newsLists'], 'id', 'id-')));
		$this->assign('reply', $reply);
		$this->display();
	}
	
	// 菜单消息自动回复
	public function menuAction() {
		$d = D("WechatReply");
		$keyWord = I('keyword', ""); empty($keyWord) and $this->error("请填写关键词");
		$condition = array(
				'rule' => $d->ruleMenu,
				'cid' => $this->aid,
				'keyword' => $keyWord,
		);
		$reply = $d->where($condition)->find();
		if (!$reply) {
			$data = array(
					'rule' => $d->ruleMenu,
					'keyword' => $keyWord,
					'cid' => $this->aid 
			);
			
			if ($d->add($data)) {
				$reply = $d->where($condition)->find();
			} else {
				$this->error('添加记录错误');
			}
		}
		$reply['typeOptions'] = $this->getOption($d->replyTypes, $reply['type']);
		$reply['newsLists'] = M('WechatMaterial')->where(array(
										'id' => array(
												'in',
												$reply['news'] 
										),
										'cid' => $this->aid 
								))->order("field(id," . $reply['news'] . ")")->select();
		$this->assign("newsJSON", empty($reply['newsLists']) ? "{}" : json_encode(addID($reply['newsLists'], 'id', 'id-')));
		$this->assign('reply', $reply);
		$this->display();
	}
	
	// 获得素材列表
	public function getNewsListPostAction() {
		$d = D('WechatMaterial');
		$materialLists = $d->getNewsList($this->aid);
		if ($materialLists || !$d->getError()) {
			$this->ajaxSuccess('', $materialLists);
		} else {
			$this->ajaxError("获取图文列表失败");
		}
	}
	// 删除关键词规则
	public function deletePostAction() {
		$this->checkAdmin(null, M('WechatReply'), "cid");
		$this->ajaxDelete('WechatReply');
	}
	
	// 设置规则状态
	public function setStatusPostAction() {
		$id = I("id", 0, "intval");
		$status = I("status", 0, "intval");
		$d = D("WechatReply");
		if ($d->where("id={$id}")->setField("status", 1 - $status) !== false) {
			$this->ajaxSuccess();
		} else {
			$this->ajaxError($d->getError());
		}
	}
	
	// 文件上传
	public function uploadAction() {
		$upload = new \Utuo\Upload();
		$info = $upload->uploadOne();
		if (!$info) {
			$this->ajaxError($upload->getError());
		} else {
			$this->ajaxSuccess("上传成功", $info);
		}
	}
}